<?php

class ChangeProfile extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->model('profile_model');
    }

    public function index() {
        echo "dasda";
    }

    public function pw() {
        $this->load->view('profile/changePassword');
    }

    public function changePW() {
        if ($this->profile_model->validateOldPw() === FALSE) {
            echo json_encode("bad_old_pw");
        } else {
            if ($this->_submit_validate_pw() === FALSE) {
                $data = array(
                    'pass' => form_error('pass'),
                    'pass2' => form_error('pass2'),
                );
                echo json_encode($data);
            } else {
                $this->profile_model->changePassword();

                echo json_encode("Ok");
            }
        }
    }

    public function changeEmail() {
        if ($this->profile_model->validateOldPwEm() === FALSE) {
            echo json_encode("bad_old_pw");
        } else {
            if ($this->_submit_validate_email() === FALSE) {
                $data = array(
                    'new_email' => form_error('new_email'),
                    'new_email2' => form_error('new_email2'),
                );
                echo json_encode($data);
            } else {
                $this->profile_model->changeEmail();

                echo json_encode("Ok");
            }
        }
    }

    public function email() {
        $this->load->view('profile/changeEmail');
    }

    private function _submit_validate_pw() {

        $this->form_validation->set_rules('pass', 'Password', 'required|min_length[6]|max_length[12]');

        $this->form_validation->set_rules('pass2', 'Confirm Password', 'required|matches[pass]');

        return $this->form_validation->run();
    }

    private function _submit_validate_email() {

        $this->form_validation->set_rules('new_email', 'E-mail', 'required|valid_email');
        $this->form_validation->set_rules('new_email2', 'E-mail', 'required|valid_email|matches[new_email]');

        return $this->form_validation->run();
    }

}

?>